﻿@page "/logger/tracking"
@inject IApiGateway iApiGateway

@inject ILogTracking ilogtracking
<div class="site-button-ghost-wrapper">
    @*<Input Placeholder="Search Document" @bind-Value="@txtValue" Bordered="false" ClassicSearchIcon />*@
    @*<Search Placeholder="Search Document" OnPressEnter="() => searchAction()" WrapperStyle="width: 200px;" @bind-Value="@searchValue" Bordered="false" ClassicSearchIcon />*@

    <Input Placeholder="Search" style="width:150px;" @bind-Value="@txtValue" />
    <RangePicker TValue="DateTime?[]" ShowTime='@("HH:mm")' OnChange="OnRangeChange" />
    <Button Type="primary" Loading="noIconLoading" Icon="search" OnClick="EnterWithIconLoading">
        Search
    </Button>
</div>
<Table @ref="table"
       TItem="LogTrackData"
       DataSource="@forecasts"
       Total="pagination.records"
       @bind-PageIndex="pagination.page"
       @bind-PageSize="pagination.rows" Loading="_loading"
       @bind-SelectedRows="selectedRows" ScrollX="1200" ScrollY="500"  OnChange="HandleTableChange">
    <RowTemplate>
        @*<Selection Key="@(context.Id.ToString())" />*@
        <Column @bind-Field="@context.Token" Width="150" />
        <Column @bind-Field="@context.Message" Ellipsis />
        <Column @bind-Field="@context.Action" Width="120"> <Tag Color="green">@context.Action</Tag> </Column>
        <Column @bind-Field="@context.Createtime" Format="yyyy-MM-dd HH:mm:ss" Width="150" />
        <Column @bind-Field="@context.Starttime" Format="yyyy-MM-dd HH:mm:ss" Width="150" />
    </RowTemplate>
    <ExpandTemplate Context="rowData">
        <div>
            <Timeline>
                @foreach (var item in rowData.Data.DataChild)
                {
                    if (item.isLast)
                    {
                        <TimelineItem Dot="dotTemplate">@item.Createtime-【@item.Action】-@item.Message</TimelineItem>
                    }
                    else
                    {
                        <TimelineItem>@item.Createtime-【@item.Action】-@item.Message</TimelineItem>
                    }

                }

                @*<TimelineItem Color="red">Solve initial network problems 2015-09-01</TimelineItem>
                    <TimelineItem>Technical testing 2015-09-01</TimelineItem>
                    <TimelineItem>Network problems being solved 2015-09-01</TimelineItem>*@
            </Timeline>
        </div>
    </ExpandTemplate>
</Table>


<style>
    .ant-form-item-label {
        flex: 0 0 130px !important;
    }

    .ant-col-offset-8 {
        margin-left: 130px;
    }

    .site-button-ghost-wrapper {
        background: rgb(190, 200, 200);
        padding: 26px 16px 16px;
    }


    .add-icon {
        border: 1px solid #abaaaa;
        color: #797777;
        border-radius: 50%;
        width: 24px;
        height: 24px;
        text-align: center;
        line-height: 24px;
        margin-top: 4px;
        margin-left: 20px;
        padding-right: 1px;
        cursor: pointer;
    }

    .searchInput {
        width: 200px;
        float: left;
        margin-left: 100px;
    }
</style>


@using System.Text.Json;
@code {
    bool noIconLoading = false;
    string txtValue { get; set; }
    DateTime? startdate { get; set; }
    DateTime? enddate { get; set; }
    WindowsHelper windows;
    string searchValue { get; set; }
    RenderFragment dotTemplate =
    @<Template>
        <Icon Type="clock-circle" Style="font-size: 16px;" />
    </Template>
    ;
    bool _loading = false;
    string _selectedValue;
    private string _sizeValue = AntSizeLDSType.Default;
    List<LogTrackData> forecasts = new List<LogTrackData>();
    IEnumerable<LogTrackData> selectedRows;
    ITable table;
    Core.DataAccess.Model.Pagination pagination = new Core.DataAccess.Model.Pagination
    {
        page = 1,
        rows =6
    };
    void Callback(string[] keys)
    {
        Console.WriteLine(string.Join(',', keys));
    }
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            windows = new WindowsHelper(jSRuntime);
            //forecasts = await GetForecastAsync();

            await GetData();

            await windows.HideLoadingAsync();
            await windows.SetTitleAsync("Tracking Logger");
            _ = InvokeAsync(StateHasChanged);
        }
    }
    public async Task GetData()
    {
        this._loading = true;
        var result = await ilogtracking.GetTracking(pagination, this.txtValue, this.startdate, this.enddate);
        var resulttoken = await ilogtracking.GetTrack();
        forecasts = new List<LogTrackData>();
        foreach (var item in result)
        {
            this.forecasts.Add(this.GetRoleDataFormat(item, resulttoken));
        }
        this._loading = false;
        this.noIconLoading = false;
    }
    private async Task HandleTableChange(QueryModel<LogTrackData> queryModel)
    {
        if (windows != null)
        {
            this.pagination.page = queryModel.PageIndex;
            this.pagination.rows = queryModel.PageSize;
            await this.LoadData();
        }
    }
    private async Task LoadData()
    {
        this._loading = true;
        await base.InvokeAsync(async () =>
        {
            await this.GetData();
            this._loading = false;
            base.StateHasChanged();
        });
    }
    async Task searchAction()
    {
        await this.GetData();
        //Console.WriteLine($"Collapsed: {this.searchValue}");
        // 搜索方法
        // await Task.Delay(100);
    }
    private async Task EnterWithIconLoading()
    {
        noIconLoading = true;
        await this.GetData();
    }
    private void OnRangeChange(DateRangeChangedEventArgs args)
    {
        startdate = args.Dates[0];
        enddate = args.Dates[1];
        //Console.WriteLine($"Selected Time: {JsonSerializer.Serialize(args.Dates)}");
        //Console.WriteLine($"Formatted Selected Time: {JsonSerializer.Serialize(args.DateStrings)}");
    }
    Task OnRowExpand(RowData<LogTrackData> rowData)
    {
        if (rowData.Data.DataChild != null)
        {
            return Task.CompletedTask;
        }


        this.StateHasChanged();
        return Task.CompletedTask;
    }
    LogTrackData GetRoleDataFormat(LogTracking item, List<LogTracking> track)
    {
        List<LogTrackData> log = new List<LogTrackData>();
        if (track.Count > 0)
        {
            var logtrack = track.Where(a => a.Token == item.Token).OrderBy(a => a.Createtime).ToList();
            int i = 0;
            foreach (var item1 in logtrack)
            {
                i++;
                log.Add(new LogTrackData
                {
                    Id = item1.Id,
                    Token = item1.Token,
                    Action = item1.Action,
                    Message = item1.Message,
                    Createtime = item1.Createtime,
                    Starttime = item1.Starttime,
                    isLast = logtrack.Count == i ? true : false,

                });
            }

        }
        var result = new LogTrackData
        {
            Id = item.Id,
            Token = item.Token,
            Action = item.Action,
            Message = item.Message,
            Createtime = item.Createtime,
            Starttime = item.Starttime,
            DataChild = log,

        };
        return result;
    }
    public class LogTrackData
    {
        public int Id { get; set; }
        public string Token { get; set; }
        public string Action { get; set; }
        public string Message { get; set; }
        public DateTime? Createtime { get; set; }
        public DateTime? Starttime { get; set; }
        public bool isLast { get; set; }
        public List<LogTrackData> DataChild { get; set; }
    }
}
